#ifndef H_CORE_THREADPOOL_H
#define H_CORE_THREADPOOL_H

#include <thread>
#include <condition_variable>
#include <mutex>
#include <memory>
#include <vector>
#include <atomic>
#include <iostream>

#include "queue.h"


namespace QinChat{
    namespace Core{
        class CTask{
            public:
                explicit CTask(){}
                void operator() (){
                    Exect();
                }

            protected:
                virtual void Exect(){
					std::cout<<"test"<<std::endl;
                }
        };

        class CThreadPool{
            public:
                explicit CThreadPool(size_t nThreadSize = 2);
                ~CThreadPool(void);

                void PushTask(std::shared_ptr<CTask>& task);

            protected:
                std::shared_ptr<CTask> GetTask();
                void WorkTask();
                void Clear(void);
                void Stop(void);

            private:
                CThreadPool(const CThreadPool&);//禁止复制拷贝.
                const CThreadPool& operator=(const CThreadPool&);

                std::vector<std::unique_ptr<std::thread> > m_workThreads;
                Queue<std::shared_ptr<CTask> > m_listTask;
                std::condition_variable m_condition;
                std::mutex m_mutex;
                std::atomic<bool> m_bExit;
                std::atomic<int>  m_idlThrNum;

        };
    }
}

#endif // ! H_CORE_THREADPOOL_H